package com.java.controller.wx;

import com.java.bean.Message;
import com.java.mvc.ResponseBody;
import com.java.mvc.ResponseView;
import com.java.util.JSONUtil;
import com.java.util.UserUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName QrcideController
 * @Description 生成二维码控制
 * @Author 0715-YuHao
 * @Date 2020/9/16 14:03
 */
public class QRCodeController {

    @ResponseView("/wx/createQRCode.do")
    public String createQRCode(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String type = request.getParameter("type");
        String code = null;
        String userPhone = null;
        String QRCodeContent = null;
        if ("express".equals(type)) {
            //快递二维码:被扫后，展示单个快递信息
            //code
            code = request.getParameter("code");
            QRCodeContent = "express_" + code;
        }else {
            //用户二维码:被扫后，快递员(柜子)端展示用户所有快递
            //userPhone
            userPhone = UserUtil.getUserPhone(session);
            QRCodeContent = "userPhone_" + userPhone;
        }
        session.setAttribute("qrcode", QRCodeContent);
        return "/wx/personQRcode.html";
    }

    @ResponseBody("/wx/qrcode.do")
    public String getQRCode(HttpServletRequest request, HttpServletResponse response) {
        HttpSession session = request.getSession();
        String qrcode = (String) session.getAttribute("qrcode");
        Message message = new Message();
        if (qrcode == null) {
            message.setStatus(-1);
            message.setContent("取件码获取异常，请重新操作！");
        }else {
            message.setStatus(0);
            message.setContent(qrcode);
        }
        return JSONUtil.toJSON(message);
    }
}
